home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
mail
/
imapcheck.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
6KB
|
212 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import imaplib
import time
import re
import email
from datetime import datetime
from pprint import pformat
from mail.emailobj import DecodedEmail
from mail import Email, AuthenticationError
from logging import getLogger
log = getLogger('imapcheck')
from util import autoassign, callsback
import traceback
class IMAPCheck(object):
def __init__(self, maxfetch = 25):
self.maxfetch = maxfetch
self.cache = { }
self.srv = None
def login(self, host, port, ssl, user, password):
autoassign(self, locals())
def update(self):
if self.srv is not None:
self.srv.logout()
self.srv = None
cls = None(getattr, imaplib if self.ssl else 'IMAP4')
srv = cls(self.host, self.port)
try:
srv.login(srv._checkquote(self.user.encode('ascii')), self.password)
except (imaplib.IMAP4.error, imaplib.IMAP4_SSL.error):
raise AuthenticationError
srv.select()
(res, lst) = srv.uid('search', None, '(UNSEEN UNDELETED)')
if res != 'OK':
raise Exception('could not retrieve unseen messages from the server')
uids = None if lst[0] else []
log.info('%d unseen uids: %r', len(uids), uids)
msg_sendtimes = { }
unread_msgs = []
if uids:
(res, lst) = srv.uid('FETCH', ','.join(uids), '(INTERNALDATE FLAGS)')
if res != 'OK':
raise Exception('could not retrieve message dates from the server')
for resp in lst:
try:
dt = getDatetime(resp)
uid = getUid(resp)
flags = getFlags(resp)
log.info('%s %r', uid, flags)
if dt is not None and uid is not None and '\\Seen' not in flags and '\\Deleted' not in flags:
unread_msgs.append((dt, uid))
msg_sendtimes[uid] = dt
continue
except Exception:
traceback.print_exc()
continue
count = len(unread_msgs)
unread_msgs.sort(reverse = True)
uids_to_fetch = []
uids_already_fetched = []
for dt, uid in unread_msgs[:self.maxfetch]:
try:
if uid not in self.cache:
uids_to_fetch += [
uid]
else:
uids_already_fetched += [
uid]
continue
except Exception:
traceback.print_exc()
continue
emailobjs = []
pref = pref
import common
if uids_to_fetch:
(res, lst) = srv.uid('fetch', ','.join(uids_to_fetch), '(BODY.PEEK[]<0.%d>)' % pref('imaplib.max_fetch_bytes', default = 5120, type = int))
if res != 'OK':
raise Exception('could not retrieve message contents from the server')
lst2 = []
currinfo = []
currdata = None
for resp in lst + [
(None, None)]:
if isinstance(resp, tuple):
if currdata:
lst2.append((currinfo, currdata))
currdata = None
currinfo = []
currinfo.append(resp[0])
currdata = resp[1]
continue
currinfo.append(resp)
for resp in lst2:
try:
if isinstance(resp, tuple):
(nfo, msg) = resp
uid = getUid(nfo)
sndtime = msg_sendtimes[uid]
emailobj = emailFromString(uid, msg, sndtime)
self.cache[uid] = emailobj
emailobjs += [
emailobj]
continue
except Exception:
traceback.print_exc()
continue
for uid in uids_already_fetched:
emailobjs += [
self.cache[uid]]
log.info('untagged responses')
log.info(pformat(srv.untagged_responses))
srv.untagged_responses.clear()
self.srv = srv
return (count, emailobjs)
def markAsRead(self, msg, callback = None):
try:
self.srv.uid('STORE', msg.id, '+FLAGS.SILENT', '(\\SEEN)')
except:
import traceback
traceback.print_exc()
return callback.error()
callback.success()
markAsRead = callsback(markAsRead)
def delete(self, msg, callback = None):
try:
self.srv.uid('STORE', msg.id, '+FLAGS.SILENT', '(\\DELETED \\SEEN)')
except:
import traceback
traceback.print_exc()
return callback.error()
callback.success()
delete = callsback(delete)
def emailFromString(uid, s, sendtime_if_error):
return Email.fromEmailMessage(uid, DecodedEmail(email.message_from_string(s)), sendtime_if_error)
def getDatetime(s):
timetuple = imaplib.Internaldate2tuple(s)
if timetuple is not None:
return datetime.fromtimestamp(time.mktime(timetuple))
_uidMatcher = re.compile('UID ([0-9]+)\\D')
def getUid(s):
if isinstance(s, basestring):
vals = [
s]
else:
vals = s
for val in vals:
match = _uidMatcher.search(val)
if match:
return match.group(1)
continue
def getFlags(s):
return imaplib.ParseFlags(s)
if __name__ == '__main__':
i = IMAPCheck()
i.login('imap.aol.com', 143, False, 'digsby04', 'thisisnotapassword')
print
print 'DONE'
print
print repr(i.update())